import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/7/29 20:24
 * @Description: TODO
 */
public class pathInZigZagTree {
    private static Stack<Integer> stack = new Stack<>();
    private static List<Integer> result = new ArrayList<>();

    public static List<Integer> pathInZigZagTree(int label) {
        int i;
        for (i = 1; ; i++) {
            if (Math.pow(2, i) > label) {
                break;
            }
        }
        int k = 0;
        if (i % 2 == 0) {
            k = (int) Math.pow(2,i) -label;
        } else {
            k = (label + 1 - (int) Math.pow(2, i - 1));
        }
        getnumbers(i, k);
        while (!stack.empty()) {
            result.add(stack.pop());
        }
        return result;
    }

    public static void getnumbers(int m, int k) {
        if (m == 0) {
            return;
        }
        if (m % 2 == 0) {
            stack.push((int) (Math.pow(2, m) - k));
        } else {
            stack.push((int) (Math.pow(2, m - 1)) + k - 1);
        }
        getnumbers(m - 1, (k + 1) / 2);
    }

    public static void main(String[] args) {
        pathInZigZagTree(26);
    }
}
